﻿using Common.Attributes;
using MODEL;
using QGZX.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace QGZX.Logic.Admin
{
    public class PositionController : Controller
    {
        //--------------------------------岗位方法-----------------------------------------
        #region 岗位列表视图 + ActionResult index()
        /// <summary>
        /// 学生列表视图
        /// </summary>
        /// <returns></returns>
        public ActionResult index()
        {
            return View();
        }
        #endregion

        #region 获取岗位列表数据 +GetPositionData()
        [HttpPost]
        [AjaxRequest]
        /// <summary>
        /// 获取岗位列表数据
        /// </summary>
        /// <returns></returns>
        public ActionResult GetPositionData()
        {                  
            //获取页容量
            int pageSize = int.Parse(Request.Form["rows"]);
            //获取请求页码
            int pageIndex = int.Parse(Request.Form["page"]);
            
            var list = OperateContext.Current.BLLSession.Iqgzx_positionBLL.GetPagedList(pageIndex, pageSize, x => x.IsDel != 1 && x.RecruitmentId==0, m => m.PositionId).Select(x => x.ToPOCO());
            int rowCount = OperateContext.Current.BLLSession.Iqgzx_positionBLL.GetListBy(x => x.PositionId != null).Count();
            
            MODEL.EasyUIModel.DataGridModel dgMode = new MODEL.EasyUIModel.DataGridModel()
            {
                total = rowCount,
                rows = list,
                footer = null
            };
            return Json(dgMode);
       } 
        #endregion

        #region 加载修改岗位 window 的html + EditPosition()
        /// <summary>
        /// 加载修改学生 window 的html
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxRequest]
        public ActionResult EditPosition(int Id)
        {
            
            //根据id 查询要修改的权限
            var model = OperateContext.Current.BLLSession.Iqgzx_positionBLL.GetListBy(x => x.PositionId ==Id).FirstOrDefault().ToPOCO().ToViewModel();
           
            string CompanyName = OperateContext.Current.BLLSession.Iqgzx_companyBLL.GetListBy(x => x.CompanyId == model.CompanyId).FirstOrDefault().CompanyName;
            ViewBag.CompanyName = CompanyName;
            //将权限对象 传给视图的model属性
            return PartialView(model);
        }
        #endregion

        #region post修改岗位信息 EditPosition(MODEL.qgzx_Position model)
        /// <summary>
        ///post修改学生信息 EditPosition(MODEL.qgzx_Position model)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult EditPosition(MODEL.qgzx_position model)
        {
            int res = OperateContext.Current.BLLSession.Iqgzx_positionBLL.Modify(model, "PositionId", "CompanyId", "CompanyName", "PositionTitle", "PositionName", "PositionType", "RecruitNum", "Treatment", "RequestGender", "WorkAddress", "WorkTime", "PositionDescrip", "ReleaseDate", "Deadlines", "OnCompanyOwn", "AuditStatus", "IsShow","IsDel", "RecruitmentId", "AuditReason", "IsPoor");
            //,"AuditStatus","IsShow","OnCompanyOwn","AuditReason","IsPoor",, "AuditReason"
            if (res > 0)
            {
                return Redirect("/admin/Position/index");
            }
            return Redirect("/admin/Position/index");
        }
        #endregion

        #region 在这新增学岗位 html代码 + AddPosition()
        /// <summary>
        /// 在这新增学生员 html代码
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxRequest]
        public ActionResult AddPosition()
        {
            return PartialView("EditPosition");
        }
        #endregion

        #region 在这新增岗位 html代码 + AddPosition(MODEL.qgzx_Position model)
        /// <summary>
        /// 在这新增岗位 html代码
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult AddPosition(MODEL.qgzx_position model)
        {

            int res = OperateContext.Current.BLLSession.Iqgzx_positionBLL.Add(model);

            if (res > 0)
            {
                return Redirect("/admin/Position/index?ok");
            }
            return Redirect("/admin/Position/index?err");
        }
        #endregion

        #region 删除岗位 + DelPosition()
        /// <summary>
        /// 删除岗位 + DelPosition()
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult DelPosition( )
        {
            try
            {
                int id = int.Parse(Request.Form["id"]);
                //qgzx_position mode = OperateContext.Current.BLLSession.Iqgzx_positionBLL.GetListBy(x => x.PositionId == id).FirstOrDefault();
                qgzx_position mode = OperateContext.Current.BLLSession.Iqgzx_positionBLL.GetListBy(x => x.PositionId == id).FirstOrDefault();
                var flag = 1;
                mode.IsDel = flag;
                int r = OperateContext.Current.BLLSession.Iqgzx_positionBLL.Modify(mode, "IsDel");
                if(r>0)
                return OperateContext.Current.RedirectAjax("ok", "删除成功~", null, "");
                else
                    return OperateContext.Current.RedirectAjax("ok", "删除失败~", null, "");

            }
            catch (Exception ex)
            {

                return OperateContext.Current.RedirectAjax("err", "您现在删除的权限正在被使用,请先取消角色中的这个权限" + ex, null, "");
            }
        }
        #endregion

        //#region 查看参与学生 + ActionResult JoinStu(int id)
        ///// <summary>
        ///// 查看参与学生
        ///// </summary>
        ///// <param name="id">学生id</param>
        ///// <returns></returns>
        //[HttpPost]
        //public ActionResult JoinStu(int id)
        //{
        //    //获取页容量
        //    int pageSize = int.Parse(Request.Form["rows"]);
        //    //获取请求页码
        //    int pageIndex = int.Parse(Request.Form["page"]);
        //    qgzx_position model = OperateContext.Current.BLLSession.Iqgzx_positionBLL.GetListBy(x => x.PositionId == id).FirstOrDefault();
        //    //查询参与岗位同学定规格字符串到List<int>中
        //    List<int> JoinStuId = model.JoinStudentsId.Split(new string[1] { "|" }, StringSplitOptions.RemoveEmptyEntries).Select(x => int.Parse(x)).ToList();
        //    //到学生表中查询所有学生返回
        //    var list = OperateContext.Current.BLLSession.Iqgzx_studentBLL.GetListBy(m => JoinStuId.Contains(m.StudentId)).Select(x => x.ToPOCO()).ToList();


        //    int rowCount = OperateContext.Current.BLLSession.Iqgzx_studentBLL.GetListBy(m => JoinStuId.Contains(m.StudentId)).Count;
        //    //return OperateContext.Current.RedirectAjax("ok", "加载成功", list, "");
        //    MODEL.EasyUIModel.DataGridModel dgMode = new MODEL.EasyUIModel.DataGridModel()
        //    {
        //        total = rowCount,
        //        rows = list,
        //        footer = null
        //    };
        //    return Json(dgMode);
        //} 
        //#endregion

        #region 查看申请学生 + ActionResult JoinStu(int id)
        /// <summary>
        /// 查看参与学生
        /// </summary>
        /// <param name="id">学生id</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult JoinStu(int id)
        {
            //获取页容量
            int pageSize = int.Parse(Request.Form["rows"]);
            //获取请求页码
            int pageIndex = int.Parse(Request.Form["page"]);
            //qgzx_position mode =OperateContext.Current.BLLSession.Iqgzx_positionBLL.GetListBy(x => x.RecruitmentId>0).FirstOrDefault();

            var list = OperateContext.Current.BLLSession.Iqgzx_position_applicationBLL.GetPagedList(pageIndex, pageSize, x => x.PositionId == id, x => x.PosApplyId);

            ////查询参与岗位同学定规格字符串到List<int>中
            //List<int> RecJoinStuId = model.RecJoinStudentsId.Split(new string[1] { "|" }, StringSplitOptions.RemoveEmptyEntries).Select(x => int.Parse(x)).ToList();
            //到学生申请表中查询所有申请学生返回
            //var list = OperateContext.Current.BLLSession.Iqgzx_position_applicationBLL.GetListBy(m => model.PosApplyId != null);


            var rowCount = OperateContext.Current.BLLSession.Iqgzx_position_applicationBLL.GetListBy(m => m.PosApplyId != null).Count;
            //return OperateContext.Current.RedirectAjax("ok", "加载成功", list, "");


            MODEL.EasyUIModel.DataGridModel dgMode = new MODEL.EasyUIModel.DataGridModel()
            {
                total = rowCount,
                rows = list,
                footer = null
            };

            return Json(dgMode);
        }

        #endregion


        //#region 审核通过或取消 通过上岗学生 + AuditStu(int id)
        ///// <summary>
        ///// 审核通过或取消 通过上岗学生
        ///// </summary>
        ///// <returns></returns>
        //[HttpPost]
        //public ActionResult AuditStu(int id)
        //{
        //    int positionId = int.Parse(Request.Form["positionId"]);
        //    qgzx_position model = OperateContext.Current.BLLSession.Iqgzx_positionBLL.GetListBy(x => x.PositionId == positionId).FirstOrDefault();
        //    //查询得到批准参与工作的学生
        //    string ApplyStudent = model.ApplyStudentsId;
        //    List<int> ApplyStudentList = ApplyStudent.Split(new string[1] { "|" }, StringSplitOptions.RemoveEmptyEntries).Select(f => int.Parse(f)).ToList();
        //    //查看是否包含该学生
        //    if (ApplyStudentList.Contains(id))
        //    {
        //        ApplyStudentList.Remove(id);
        //    }
        //    else
        //    {
        //        ApplyStudentList.Add(id);
        //    }
        //    string list = "";
        //    foreach (int i in ApplyStudentList)
        //    {
        //        list += "|";
        //        list += i;
        //    }
        //    model.ApplyStudentsId = list;

        //    OperateContext.Current.BLLSession.Iqgzx_positionBLL.ModifyBy(model, x => x.PositionId == model.PositionId, "ApplyStudentsId");
        //    return OperateContext.Current.RedirectAjax("ok", "审核通过~", null, "");
        //} 
        //#endregion

        #region 商家筛选上岗学生 + ActionResult CompanyChoseStu()
        /// <summary>
        ///  商家筛选上岗学生
        /// </summary>
        /// <returns></returns>

        public ActionResult CompanyChooseStu(int id)
        {
            var model = OperateContext.Current.BLLSession.Iqgzx_position_applicationBLL.GetListBy(x => x.PosApplyId == id);

            return View(model);
        }
        #endregion
      

        #region 岗位详细信息 + ActionResult Detail()
        /// <summary>
        /// 岗位详细信息 + ActionResult Detail(int id)
        /// </summary>
        /// <returns></returns>
        public ActionResult Detail(int id)
        {
            var model = OperateContext.Current.BLLSession.Iqgzx_positionBLL.GetListBy(x => x.PositionId == id).FirstOrDefault().ToViewModel();

            return View(model);
        } 
        #endregion

        // ---------------------------------单位方法-----------------------------------------

        #region 单位列表视图 + ActionResult Company()
        /// <summary>
        /// 学生列表视图
        /// </summary>
        /// <returns></returns>
        public ActionResult Company()
        {
            return View();
        }
        #endregion

        #region 获取单位列表数据 +GetCompanyData()
        [HttpPost]
        [AjaxRequest]
        /// <summary>
        /// 获取单位列表数据
        /// </summary>
        /// <returns></returns>
        public ActionResult GetCompanyData()
        {
            //获取页容量
            int pageSize = int.Parse(Request.Form["rows"]);
            //获取请求页码
            int pageIndex = int.Parse(Request.Form["page"]);
            var list = OperateContext.Current.BLLSession.Iqgzx_companyBLL.GetPagedList(pageIndex, pageSize, x => x.CompanyId != null, m => m.CompanyId).Select(x => x.ToPOCO());
            int rowCount = OperateContext.Current.BLLSession.Iqgzx_companyBLL.GetListBy(x => x.CompanyId != null).Count();
            //return OperateContext.Current.RedirectAjax("ok", "加载成功", list, "");
            MODEL.EasyUIModel.DataGridModel dgMode = new MODEL.EasyUIModel.DataGridModel()
            {
                total = rowCount,
                rows = list,
                footer = null
            };
            return Json(dgMode);
        }
        #endregion

        #region 加载修改单位 window 的html + EditCompany()
        /// <summary>
        /// 加载修改学生 window 的html
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxRequest]
        public ActionResult EditCompany(int id)
        {
            //根据id 查询要修改的权限
            var model = OperateContext.Current.BLLSession.Iqgzx_companyBLL.GetListBy(x => x.CompanyId == id).FirstOrDefault().ToViewModel();
            //将权限对象 传给视图的model属性
            return PartialView(model);
        }
        #endregion

        #region post修改单位信息 EditCompany(MODEL.qgzx_company model)
        /// <summary>
        ///post修改单位信息 EditCompany(MODEL.qgzx_company model)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult EditCompany(MODEL.qgzx_company model)
        {
            int res = OperateContext.Current.BLLSession.Iqgzx_companyBLL.Modify(model, "CompanyId", "CompanyName", "CompanyCode", "CompanyType", "CompanyField", "CompanyAddress", "CompanyPhone", "Comtact", "ContactPhone", "CompanyIntro", "LoginName", "LoginPassword","IsAudit","IsBlackList","AuditTime","AuditReason","BusinessLicense");
            if (res > 0)
            {
                return Redirect("/admin/Position/Company");
            }
            return Redirect("/admin/Position/Company");
        }
        #endregion

        #region 在这新增学单位 html代码 + AddCompany()
        /// <summary>
        /// 在这新增单位 html代码
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxRequest]
        public ActionResult AddCompany()
        {
            return PartialView("EditCompany");
        }
        #endregion

        #region 在这新增单位 html代码 + AddCompany(MODEL.qgzx_company model)
        /// <summary>
        /// 在这新增单位 html代码
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult AddCompany(MODEL.qgzx_company model)
        {

            int res = OperateContext.Current.BLLSession.Iqgzx_companyBLL.Add(model);

            if (res > 0)
            {
                return Redirect("/admin/Position/Company?ok");
            }
            return Redirect("/admin/Position/Company?err");
        }
        #endregion

        #region 删除单位 + DelCompany()
        /// <summary>
        /// 删除单位 + DelCompany()
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult DelCompany()
        {
            try
            {
                int id = int.Parse(Request.Form["id"]);
                OperateContext.Current.BLLSession.Iqgzx_companyBLL.DelBy(x => x.CompanyId == id);
                return OperateContext.Current.RedirectAjax("ok", "删除成功~", null, "");
            }
            catch (Exception ex)
            {

                return OperateContext.Current.RedirectAjax("err", "出错了~~~~~~您现在删除的单位正在被使用" + ex, null, "");
            }
        }
        #endregion

        //----------------------------------业务方法---------------------------------------------
        #region [HttpGet]发布岗位信息填写视图 +  ReleasePosition()
        /// <summary>
        /// [HttpGet]发布岗位信息填写视图 +  ReleasePosition()
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult ReleasePosition()
        {
            qgzx_admin current = OperateContext.Current.Admin;
            qgzx_company model = OperateContext.Current.BLLSession.Iqgzx_companyBLL.GetListBy(x => x.CompanyName == current.LoginName).FirstOrDefault();
            if (model == null)
            {
                return OperateContext.Current.RedirectAjax("err", "没有权限或者不属于公司单位", null, "");
            }
            else
            {
                return View();
            }
        } 
        #endregion

        #region 发布岗位 业务 + ReleasePosition(MODEL.qgzx_position model)
        /// <summary>
        ///发布岗位 业务
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ValidateInput(false)]
        [AjaxRequest]
        public ActionResult ReleasePosition(MODEL.qgzx_position model)
        {
            int CompareId = OperateContext.Current.BLLSession.Iqgzx_companyBLL.GetListBy(x=>x.CompanyName==OperateContext.Current.Admin.LoginName).First().CompanyId;

            model.CompanyId = CompareId;
            model.ReleaseDate = DateTime.Now;
            model.RecruitmentId = 0;
            OperateContext.Current.BLLSession.Iqgzx_positionBLL.Add(model);
            return OperateContext.Current.RedirectAjax("ok", "发布岗位成功，请等待审核", null, "");
        } 
        #endregion

        #region 高级搜索(比较笨，先用这个方法) + ActionResult Search()
        /// <summary>
        /// 高级搜索(比较笨，先用这个方法)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Search()
        {
            string CompanyName = Request.QueryString["CompanyName"];
            string PositionName = Request.QueryString["PositionName"];
            string PositionType = Request.QueryString["PositionType"];
            string AuditStatus = Request.QueryString["AuditStatus"];
          
            //获取页容量
            int pageSize = int.Parse(Request.Form["rows"]);
            //获取请求页码
            int pageIndex = int.Parse(Request.Form["page"]);
            //高级搜索的功能！！！
            
            var list = OperateContext.Current.BLLSession.Iqgzx_positionBLL.GetListBy(x => x.PositionId != null).ToList().Select(x=>x.ToPOCO());
            if (CompanyName != "")
            {
                list = list.Where(x => x.CompanyName.ToString().Contains(CompanyName)).ToList();
            }
            if (PositionName != "")
            {
                list = list.Where(x => x.PositionName.Contains(PositionName)).ToList();
            }
            if (PositionType != "")
            {
                list = list.Where(x => x.PositionType.Contains(PositionType)).ToList();
            }
          
           
            
            int rowCount = list.Count();
            //return OperateContext.Current.RedirectAjax("ok", "加载成功", list, "");
            MODEL.EasyUIModel.DataGridModel dgMode = new MODEL.EasyUIModel.DataGridModel()
            {
                total = rowCount,
                rows = list,
                footer = null
            };
            return Json(dgMode);
        }
        #endregion

        #region 高级搜索单位(比较笨，先用这个方法) + ActionResult SearchCompany()
        /// <summary>
        /// 高级搜索(比较笨，先用这个方法)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SearchCompany()
        {
            string CompanyId = Request.QueryString["CompanyId"];
            string CompanyName = Request.QueryString["CompanyName"];
            string CompanyType = Request.QueryString["CompanyType"];
            string CompanyField = Request.QueryString["CompanyField"];
            //获取页容量
            int pageSize = int.Parse(Request.Form["rows"]);
            //获取请求页码
            int pageIndex = int.Parse(Request.Form["page"]);
            //高级搜索的功能！！！
            var list = OperateContext.Current.BLLSession.Iqgzx_companyBLL.GetListBy(x => x.CompanyId != null).ToList().Select(x => x.ToPOCO());
            if (CompanyId != "")
            {
                list = list.Where(x => x.CompanyId.ToString().Contains(CompanyId)).ToList();
            }
            if (CompanyName != "")
            {
                list = list.Where(x => x.CompanyName.Contains(CompanyName)).ToList();
            }
            if (CompanyType != "")
            {
                list = list.Where(x => x.CompanyType.ToString().Contains(CompanyType)).ToList();
            }
            if (CompanyField != "")
            {
                list = list.Where(x => x.CompanyField.ToString().Contains(CompanyField)).ToList();
            }

            int rowCount = list.Count();
            //return OperateContext.Current.RedirectAjax("ok", "加载成功", list, "");
            MODEL.EasyUIModel.DataGridModel dgMode = new MODEL.EasyUIModel.DataGridModel()
            {
                total = rowCount,
                rows = list,
                footer = null
            };
            return Json(dgMode);
        }
        #endregion
    }
}
